package edu.ynu.hhy.architectureclass.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import edu.ynu.hhy.architectureclass.common.entity.LogicEntity;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@Getter
@Setter
@Entity
@Where(clause = "is_deleted = 0")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class SensorData extends LogicEntity implements Serializable {

    @Column(nullable = false)
    private Double value; // 传感器值

    @Column(nullable = false)
    private String name;

    @Column(name = "device_serial", nullable = false) // 确保列名与数据库一致
    private String deviceSerial; // 设备的序列号

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "device_serial", referencedColumnName = "serial_number", insertable = false, updatable = false)
    @JsonIgnoreProperties("sensorDataList")
    private Device device; // 关联的设备

    @Temporal(TemporalType.TIMESTAMP)
    @Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
    private Date createTime; // 创建时间

    @Override
    public String toString() {
        return "SensorData{" +
                "id=" + getId() +
                ", value=" + value +
                ", deviceSerial='" + deviceSerial + '\'' +
                ", device=" + (device != null ? device.getSerialNumber() : null) +
                ", createTime=" + getCreateTime() +
                ", updateTime=" + getUpdateTime() +
                ", isDeleted=" + getIsDeleted() +
                '}';
    }
}